import { Effect, Reducer, request } from 'umi';

const DemoModel = {
    namespace: 'demo',
    state: {
        data: {},
    },
    effects: {
        *fetch({ type, payload }, { put, call }) {
            const data = yield request('/api/items')
            yield put({
                type: 'save',
                payload: {
                    data: data,
                },
            });
        },
    },
    reducers: {
        save(state, action) {
            return {
                ...state, ...action.payload,
            };
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname, query }) => {
                if (pathname === '/') {
                    dispatch({
                        type: 'fetch'
                    })
                }
            })
        }
    }
};

export default DemoModel;